package com.fmdj.mis.api.service.Impl;

import cn.hutool.core.map.MapUtil;
import com.fmdj.common.util.PageUtils;
import com.fmdj.mis.api.db.dao.RoleDao;
import com.fmdj.mis.api.db.pojo.RoleEntity;
import com.fmdj.mis.api.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Override
    public PageUtils searchRoleByPage(HashMap param) {
        //计算起始索引
        Integer page = MapUtil.getInt(param, "page");
        Integer length = MapUtil.getInt(param, "length");
        Integer start = (page - 1) * length;
        param.put("start", start);

        ArrayList<HashMap> list = roleDao.searchRoleByPage(param);

        long count = roleDao.searchRoleCount(param);

        return new PageUtils(list,count,page,length);
    }

    @Override
    public ArrayList<HashMap> searchAllRole() {
        return roleDao.searchAllRole();
    }

    @Override
    public HashMap searchById(int id) {
        return roleDao.searchById(id);
    }

    @Override
    public int insertRole(RoleEntity role) {
        return roleDao.insert(role);
    }

    @Override
    public int updateRole(RoleEntity role) {
        return roleDao.update(role);
    }

    @Override
    public ArrayList<Integer> searchUserIdByRoleId(int roleId) {
        return roleDao.searchUserIdByRoleId(roleId);
    }

    @Override
    public int deleteRoleByIds(Integer[] ids) {
        Integer rows = null;

        if (!roleDao.searchCanDelete(ids)) {
            rows = roleDao.deleteRoleByIds(ids);
        }

        return rows;
    }

}
